/* global Cesium viewer */
const P = Math.PI / 180;
let layerArr = [];
let imageryLayers = null;
let ICENTER_URL = 'http://36.112.11.166:38310';

export function addImageryLayer(item, flag) {
    imageryLayers = viewer.scene.imageryLayers;
    if (flag) {
        imageryLayers.add(
            new Cesium.ImageryLayer(
                new Cesium.C_WebMapTileServiceImageryProvider({
                    url: ICENTER_URL + '/geoserverplus/gwc/service/wmts',
                    layer: 'icenter:' + item.layerName,
                    style: 'default',
                    format: 'image/' + item.mimeType.toLowerCase(),
                    tileMatrixSetID: item.gridSet,
                    tilingScheme:
                        item.gridSet === 'EPSG:4326'
                            ? new Cesium.GeographicTilingScheme()
                            : new Cesium.WebMercatorTilingScheme(),
                    maximumLevel: item.maxLevel,
                }),
                {
                    minimumTerrainLevel: item.minLevel,
                    rectangle: new Cesium.Rectangle(
                        item.minX * P,
                        item.minY * P,
                        item.maxX * P,
                        item.maxY * P
                    ),
                }
            )
        );
    } else {
        for (let i = 0; i < imageryLayers.length; ++i) {
            if (imageryLayers._layers[i].imageryProvider._layer === 'icenter:' + item.layerName) {
                imageryLayers.remove(imageryLayers._layers[i]);
                break;
            }
        }
    }
}

export function changeClockTime(time) {
    imageryLayers = viewer.scene.imageryLayers;
    if (!time) {
        throw new Error('请传入Iso8601格式的时间参数');
    }
    layerArr.forEach(item => {
        let eleImg = imageryLayers._layers.find(itemTwo => itemTwo.id === 'sxyx-' + item.layerName);
        if (time === item.timeN) {
            eleImg.show = true;
        } else {
            eleImg.show = false;
        }
    });
}
// 添加影像图
export function addOrRemoveLayer(ele, isAdd) {
    Cesium.C_layerManager.setBaseLayer(ele, isAdd);
}
// 隐藏所有影像图
export function hiddenAllLayer() {
    imageryLayers._layers.forEach(itemTwo => {
        if (itemTwo.id.includes('sxyx-')) {
            itemTwo.show = false;
        }
    });
}
export function addImageryLayerWithTime(flag, item = {}, params = {}) {
    imageryLayers = viewer.scene.imageryLayers;
    hiddenAllLayer();
    if (flag) {
        layerArr = [...params.layerData];
        if (!params.times || !item.layerName) {
            throw new Error('缺少必要的参数');
        }
        params.layerData.forEach(itemLayer => {
            let eleImg = imageryLayers._layers.find(
                itemTwo => itemTwo.id === 'sxyx-' + itemLayer.layerName
            );
            if (eleImg) {
                imageryLayers.remove(eleImg);
            }
            let param = {
                methodName: 'setBaseLayer',
                label: '时序影像',
                type: 'WMTS',
                preloading: true,
                id: 'sxyx-' + itemLayer.layerName,
                provider: {
                    url: ICENTER_URL + '/tilecache/service/wmts',
                    layer: itemLayer.layerName,
                    format: 'image/png',
                    style: 'default',
                    maximumLevel: itemLayer.maxLevel,
                    tileMatrixSetID: 'EPSG:4326',
                    projection: 'WGS84',
                },
            };
            addOrRemoveLayer(param, true);
        });
        changeClockTime(params.times[0]);
    }
}
